﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;

namespace StoreApp.Models
{
    class StoreAppDbInitializer : DropCreateDatabaseIfModelChanges<StoreAppDbContext>
    {
        // Number of products to be seeded.
        int NumberOfProducts = 30;

        Random random;

        public StoreAppDbInitializer()
        {
            random = new Random();
        }
        protected override void Seed(StoreAppDbContext context)
        {
            SeedProducts(context);
            base.Seed(context);
        }

        private void SeedProducts(StoreAppDbContext context)
        {
            List<Product> products = new List<Product>();

            for (int i = 0; i < NumberOfProducts; i++)
            {
                products.Add(new Product() { Name = GetProductName() + i, Price = RandomNumber(1, 20) });
            }

            products.ForEach(a => context.Products.Add(a));
            context.SaveChanges();

        }

        #region RandomProductNameProducers

        private string GetProductName()
        {
            int contentNum = RandomNumber(0, 5);
            return GetProductNames()[contentNum];
        }

        private int RandomNumber(int min, int max)
        {
            return random.Next(min, max);
        }

        public static string[] GetProductNames()
        {
            var products = new string[] { "Elma", "Armut", "Araba", "Giysi", "Blah", "Foo" };
            return products;
        }

        #endregion

    }
}
